package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dto.SensitiveDto;
import com.heima.model.admin.pojo.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author TheCai
 * @date 2021/5/24 11:29
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive>
    implements AdSensitiveService {

  /**
   * 敏感词分页列表
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult list(SensitiveDto dto) {
    // 1.参数检测
    dto.checkParam();
    // 2.按名称模糊分页查询
    IPage<AdSensitive> pageParam = new Page(dto.getPage(), dto.getSize());
    //
    LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();

    if (StringUtils.isNotBlank(dto.getName())) {
      lambdaQueryWrapper.like(AdSensitive::getSensitives, dto.getName());
    }

    // 执行查询得到分页结果

    IPage result = page(pageParam, lambdaQueryWrapper);

    // 3.封装PageResonseResult
    ResponseResult responseResult =
        new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());

    responseResult.setData(result.getRecords());

    return responseResult;
  }

  /**
   * 新增
   *
   * @param adSensitive
   * @return
   */
  @Override
  public ResponseResult insert(AdSensitive adSensitive) {
    // 检查数据
    if (StringUtils.isBlank(adSensitive.getSensitives())) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
    // 执行新增
    AdSensitive sensitive = getById(adSensitive.getId());
    if (sensitive!=null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在");
    }
    adSensitive.setCreatedTime(new Date());
    save(adSensitive);

    // 返回响应
    return ResponseResult.okResult();
  }

  /**
   * 修改
   * @param adSensitive
   * @return
   */
  @Override
  public ResponseResult amend(AdSensitive adSensitive) {
    // 检查数据
    if (null == adSensitive || adSensitive.getId() == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
    AdSensitive sensitive = this.getById(adSensitive.getId());
    if (null == sensitive) {
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    // 执行
    this.updateById(adSensitive);
    // 返回响应

    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
  }

  /**
   * 根据id删除
   * @param id
   * @return
   */
  @Override
  public ResponseResult deleteById(Integer id) {
    if (null == id) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
    AdSensitive adSensitive = getById(id);
    if (adSensitive == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    removeById(id);
    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
  }
}
